/**
 * $Id: ReportReciveThread.java,v 1.17 2006/10/23 01:44:21 martin Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.runner.process;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.Queue;
import com.haoxi.gateway.common.SQLConst;
import com.haoxi.gateway.common.SYSConst;
import com.haoxi.gateway.runner.AbsOutQueue;
import com.haoxi.gateway.runner.RunnerMgr;
import com.haoxi.gateway.runner.smscommand.SubmitReportCmd;
import com.haoxi.gateway.util.db.DBConnectUtil;

/**
 * MT Report接收线程
 * 
 * Revision History
 * 
 * 2006-5-29,star.Zhao,created it
 */
public class ReportReciveThread extends AbsOutQueue {
    private static final Log log = LogFactory.getLog(ReportReciveThread.class);
    private static final String MY_NAME = "ReportReciveThread";

    public ReportReciveThread(Queue inQueue) {
        super(inQueue);
        setName(MY_NAME);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.haoxi.gateway.common.AbsOutQueue#doJob(java.lang.Object, int)
     */
    public int doJob(Object pObject, int queueLen) {
        int nRet = AbsOutQueue.PROC_ERROR;
        log.debug(pObject.toString());
        if (pObject instanceof SubmitReportCmd) {
            SubmitReportCmd report = (SubmitReportCmd) pObject;
            if (!saveReportIntoTempDB(report)) {
                log
                        .error("save recieved MT report unsuccessfully in ReportReciveThread.doJob()!");
                return nRet;
            }
            log.debug("Deal Report successfully! " + report.toString());
            nRet = AbsOutQueue.PROC_NORMAL;
        } else {
            // unkown object message !!!!!!!!!!!!!!!!!!!!!
            log.error("have unkown object message !!!!!!:" + pObject);
            nRet = AbsOutQueue.PROC_ERROR;
        }

        return nRet;
    }

    /**
     * 把接收到Report数据存储到ReportTemp表中，
     * 
     * @param report
     * @return
     */
    private boolean saveReportIntoTempDB(SubmitReportCmd report) {
        boolean returnDB = false;
        Connection conn = null;
        PreparedStatement pst = null;
        String gateway_DBName = (String) RunnerMgr.getInstance()
                .getSystemConf().get(SYSConst.CONF_DB_MYSQL);
        try {
            conn = DBConnectUtil.open(gateway_DBName);
            pst = conn.prepareStatement(SQLConst.SAVE_MT_REPORT_DATA);

            // save MSGID,MOBILE,MOBILETYPE,status,SAVETIME
            pst.setString(1, report.getMsgID());
            pst.setString(2, report.getUserNumber());
            pst.setInt(3, report.getUserNumberType());
            String status = report.getReport();
            if (null == status || "".equals(status.trim())) {
                status = "EXPIRED";
            }
            pst.setString(4, status);
            int num = pst.executeUpdate();
            if (1 == num) {
                returnDB = true;
            }
        } catch (SQLException sqle) {
            log.error("SQLException in save_operate, SubmitReportCmd:  "
                    + report.toString());
            log
                    .error(
                            "SQLException in ReportReciveThread.saveReportIntoTempDB()!",
                            sqle);
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException sqle) {
                log
                        .error(
                                "release db connection unsuccessfully in ReportReciveThread.saveReportIntoTempDB()!",
                                sqle);
            }
        }
        return returnDB;
    }

}
